<?php

namespace App\Http\Controllers\Front;



use App\Interfaces\PictureService;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Services\Sms;

class HomeController extends Controller
{


    public function index(PictureService $pictureService,Sms $sms)
    {
//        $sms->sendMsg(1,23,13311197067,'测试全流程短信');
//        exit();

        $is_device = '';

        if($this->check_wap()){

            $is_device = 'wap';
        }else{

            $is_device = 'web';
        }

        // if ($is_device == 'web' && $_SERVER['HTTP_HOST'] == 'm.afuyigou.com') {          //www.afuyigou.com
        if ($is_device == 'web' && $_SERVER['HTTP_HOST'] == env('MOBILE_DOMAIN')) {          //www.afuyigou.com

            return redirect('http://'.env('PC_DOMAIN','yigou.mengzhijy.com'));
        }elseif ($is_device == 'wap' && $_SERVER['HTTP_HOST'] == env('PC_DOMAIN')) {          //m.afuyigou.com

            return redirect('http://'.env('MOBILE_DOMAIN','myigou.mengzhijy.com'));
        }

        return view('front.home.show');
    }


    public function check_wap(){
        if (isset($_SERVER['HTTP_VIA'])) return true;
        if (isset($_SERVER['HTTP_X_NOKIA_CONNECTION_MODE'])) return true;
        if (isset($_SERVER['HTTP_X_UP_CALLING_LINE_ID'])) return true;
        if (strpos(strtoupper($_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0) {
            // Check whether the browser/gateway says it accepts WML.
            $br = "WML";
        } else {
            $browser = isset($_SERVER['HTTP_USER_AGENT']) ? trim($_SERVER['HTTP_USER_AGENT']) : '';
            if(empty($browser)) return true;
            $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');

            $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');

            $found_mobile=$this->checkSubstrs($mobile_os_list,$browser) || $this->checkSubstrs($mobile_token_list,$browser);
            if($found_mobile)
                $br ="WML";
            else $br = "WWW";
        }
        if($br == "WML") {
            return true;
        } else {
            return false;
        }
    }

    public function checkSubstrs($list,$str){
        $flag = false;
        for($i=0;$i<count($list);$i++){
            if(strpos($str,$list[$i]) > 0){
                $flag = true;
                break;
            }
        }
        return $flag;
    }

}
